home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MEMORY.SWG / 0009_MEMALLOC.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  5KB  |  103 lines

  1. {This is a Unit MEMALLOC.PAS For use With the .VOC player...}
  2. Unit MemAlloc;
  3.  
  4. { Purpose is to provide the ability to create (destroy) dynamic Variables  }
  5. { without needing to reserve heap space at Compile time.                   }
  6.  
  7. Interface
  8.  
  9. Function Malloc(Var Ptr; Size : Word) : Word;
  10. { Allocate free memory and return a Pointer to it.  The amount of memory
  11. { requested from Dos is calculated as (Size/4)+1 paraGraphs.  if the
  12. { allocation is successful, the unTyped Var parameter Ptr will be populated
  13. { With the address of the allocated memory block, and the Function will return}
  14. { a zero result.  Should the request to Dos fail, Ptr will be populated with
  15. { the value NIL, and the Function will return the appropriate Dos error code.
  16. }
  17.  
  18. Function Dalloc(Var Ptr) : Word;
  19. { Deallocate the memory pointed to by the unTyped Var parameter Ptr
  20. }
  21.  
  22. Function DosMemAvail : LongInt;
  23. { Return the size of the largest contiguous chuck of memory available For use
  24. }
  25.  
  26. { ---------------------------------------------------------------------------
  27. }
  28.  
  29. Implementation
  30.  
  31. { ---------------------------------------------------------------------------
  32. }
  33.  
  34. Function Malloc(Var Ptr; Size : Word) : Word;
  35. begin
  36.    Inline(
  37.      $8B/$46/<SIZE/         {            mov         ax,[bp+<Size]}
  38.      $B9/$04/$00/           {            mov         cx,4}
  39.      $D3/$E8/               {            shr         ax,cl}
  40.      $40/                   {            inc         ax}
  41.      $89/$C3/               {            mov         bx,ax}
  42.      $B4/$48/               {            mov         ah,$48}
  43.      $CD/$21/               {            int         $21             ;Allocate memory}
  44.      $72/$07/               {            jc          AllocErr        ;if any errors ....}
  45.      $C7/$46/$FE/$00/$00/   {NoErrors:   mov Word    [bp-2],0        ;Return 0 For successful allocation}
  46.      $EB/$05/               {            jmp short   Exit}
  47.      $89/$46/$FE/           {AllocErr:   mov         [bp-2],ax       ;Return error code}
  48.      $31/$C0/               {            xor         ax,ax           ;Store a NIL value into the ptr}
  49.      $C4/$7E/<PTR/          {Exit:       les         di,[bp+<Ptr]    ;Address of Pointer into es:di}
  50.      $50/                   {            push        ax              ;Save the Segment part}
  51.      $31/$C0/               {            xor         ax,ax           ;offset is always 0}
  52.      $FC/                   {            cld                         ;Make sure direction is upward}
  53.      $AB/                   {            stosw                       ;Store offset of memory block}
  54.      $58/                   {            pop         ax              ;Get back segment part}
  55.      $AB);                  {            stosw                       ;Store segment of memory block}
  56.  
  57. end {Malloc};
  58.  
  59. { ---------------------------------------------------------------------------
  60. }
  61.  
  62. Function Dalloc(Var Ptr) : Word;
  63. begin
  64.    if Pointer(Ptr) <> NIL then begin
  65.       Inline(
  66.         $B4/$49/               {            mov         ah,$49}
  67.         $C4/$7E/<PTR/          {            les         di,[bp+<Ptr]}
  68.         $26/$C4/$3D/           {        es: les         di,[di]}
  69.         $CD/$21/               {            int         $21}
  70.         $72/$02/               {            jc          Exit}
  71.         $31/$C0/               {NoError:    xor         ax,ax}
  72.         $89/$46/$FE);          {Exit:       mov         [bp-2],ax}
  73.       Pointer(Ptr) := NIL;
  74.    end {if}
  75.    else
  76.       Dalloc := 0;
  77. end {Dealloc};
  78.  
  79. { ---------------------------------------------------------------------------
  80. }
  81.  
  82. Function DosMemAvail : LongInt;
  83. begin
  84.    Inline(
  85.      $BB/$FF/$FF/           {         mov         bx,$FFFF}
  86.      $B4/$48/               {         mov         ah,$48}
  87.      $CD/$21/               {         int         $21}
  88.      $89/$D8/               {         mov         ax,bx}
  89.      $B9/$10/$00/           {         mov         cx,16}
  90.      $F7/$E1/               {         mul         cx}
  91.      $89/$46/$FC/           {         mov         [bp-4],ax}
  92.      $89/$56/$FE);          {         mov         [bp-2],dx}
  93. end; {DosMemAvail}
  94.  
  95. end. {Unit MemAlloc}
  96.  
  97. {Ok.. The Code can be rewritten to use GetMem and FreeMem (in fact I suggest
  98. you do this). I rewrote it myself to do so, but this is the distribution copy.
  99. (I made one change in line 316-318 of SBVOICE.PAS bumping up the driver size
  100. from 3000 to 5000 to accomodate the SoundBlaster 2.0 driver)
  101. This Program requires CT-VOICE.DRV which is distributed With the Soundblaster.
  102. }
  103.